import { snapshot_UNSTABLE } from 'recoil';
import { Howl } from 'howler';
import { CONFIG } from '@/state';

let base = '';
if (process.env.NODE_ENV === 'development') {
  base = '/mine';
}

export const soundUrl = [
  `${base}/voice/button_click.mp3`,
  `${base}/voice/chip.mp3`,
  `${base}/voice/bet_click.mp3`,
  `${base}/voice/soft_win.mp3`,
  `${base}/voice/bomb_detect.mp3`,
  `${base}/voice/mines_select.mp3`,
  `${base}/voice/win.mp3`,
];

const buttonClickSound = new Howl({
  src: soundUrl[0],
});
export const playButtonClickSound = () => {
  const snapshot = snapshot_UNSTABLE();
  const config = snapshot.getLoadable(CONFIG).contents;
  if (config.sound) {
    buttonClickSound.play();
  }
};

const chipSound = new Howl({
  src: soundUrl[1],
});
export const playChipSoundSound = () => {
  const snapshot = snapshot_UNSTABLE();
  const config = snapshot.getLoadable(CONFIG).contents;
  if (config.sound) {
    chipSound.play();
  }
};

const betClickSound = new Howl({
  src: soundUrl[2],
});
export const playBetClickSound = () => {
  const snapshot = snapshot_UNSTABLE();
  const config = snapshot.getLoadable(CONFIG).contents;
  if (config.sound) {
    betClickSound.play();
  }
};

const softWinSound = new Howl({
  src: soundUrl[3],
});
export const playSoftWinSound = () => {
  const snapshot = snapshot_UNSTABLE();
  const config = snapshot.getLoadable(CONFIG).contents;
  if (config.sound) {
    softWinSound.play();
  }
};

const bombDetectSound = new Howl({
  src: soundUrl[4],
});
export const playBombDetectSound = () => {
  const snapshot = snapshot_UNSTABLE();
  const config = snapshot.getLoadable(CONFIG).contents;
  if (config.sound) {
    bombDetectSound.play();
  }
};

const minesSelectSound = new Howl({
  src: soundUrl[5],
});
export const playMinesSelectSound = () => {
  const snapshot = snapshot_UNSTABLE();
  const config = snapshot.getLoadable(CONFIG).contents;
  if (config.sound) {
    minesSelectSound.play();
  }
};

const winSound = new Howl({
  src: soundUrl[6],
});
export const playWinSound = () => {
  const snapshot = snapshot_UNSTABLE();
  const config = snapshot.getLoadable(CONFIG).contents;
  if (config.sound) {
    winSound.play();
  }
};